//
//  FriendListView.m
//  MacDevTest
//
//  Created by 夏利兵 on 2020/8/13.
//  Copyright © 2020 com.lbx. All rights reserved.
//

#import "FriendListView.h"
#import <Masonry/Masonry.h>
#import "SWSTAnswerButton.h"

@interface FriendListView()

@property (nonatomic, strong) NSTrackingArea *area;
@property (nonatomic, strong) NSEvent *leftMouseDownEvent;
@property (nonatomic, strong) NSEvent *rightMouseDownEvent;
@property (nonatomic, strong) SWSTAnswerButton *closeBtn;
@end

@implementation FriendListView

- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];
    
    // Drawing code here.
}

- (id)initWithFrame:(NSRect)frameRect
{
    if (self = [super initWithFrame:frameRect]) {
        
        _closeBtn = [SWSTAnswerButton buttonWithImage:[NSImage imageNamed:@"AIO_Tab_Close_Normal"] target:self action:@selector(close)];
        _closeBtn.bordered = NO;
        
        _closeBtn.normalImage = [NSImage imageNamed:@"AIO_Tab_Close_Normal"];
        _closeBtn.hoverImage = [NSImage imageNamed:@"AIO_Tab_Close_Hover"];
        _closeBtn.highlightImage = [NSImage imageNamed:@"AIO_Tab_Close_Down"];
        
        _userImageView = [[NSImageView alloc]init];
        _userImageView.imageScaling = NSImageScaleProportionallyDown;
        _userImageView.wantsLayer = YES;
        //宽度的一半(宽和高相等)即能达到圆形效果
        _userImageView.layer.cornerRadius = 20;
        _userImageView.layer.backgroundColor = [NSColor whiteColor].CGColor;
        
        
        _userNickName = [[NSTextField alloc]init];
        _userNickName.bordered = NO;
        _userNickName.editable = NO;
        _userNickName.selectable  =NO;
        _userNickName.alignment = NSTextAlignmentLeft;
        _userNickName.backgroundColor = [NSColor clearColor];
        _userNickName.font = [NSFont systemFontOfSize:14];
        _userNickName.textColor = [NSColor blackColor];
        
        _detailInfo = [[NSTextField alloc]init];
        _detailInfo.bordered = NO;
        _detailInfo.editable = NO;
        _detailInfo.selectable  =NO;
        _detailInfo.alignment = NSTextAlignmentLeft;
        _detailInfo.backgroundColor = [NSColor clearColor];
        _detailInfo.font = [NSFont systemFontOfSize:12];
        _detailInfo.textColor = [NSColor grayColor];
        
        
        [self addSubview:_closeBtn];
        [self addSubview:_userImageView];
        [self addSubview:_userNickName];
        [self addSubview:_detailInfo];
        
        [_closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(6);
            make.centerY.equalTo(self);
            make.width.height.mas_offset(16);
        }];
        
        _closeBtn.hidden = YES;
        
        [_userImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.closeBtn.mas_right).offset(2);
            make.centerY.equalTo(self);
//            make.top.equalTo(self).offset(10);
//            make.bottom.equalTo(self).offset(-10);
            make.width.height.mas_equalTo(40);
        }];
        
        [_userNickName mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(self.userImageView.mas_right).offset(10);
            make.top.equalTo(self.userImageView);
            make.right.equalTo(self);
            make.height.mas_equalTo(20);
        }];
        
        [_detailInfo mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(self.userNickName);
            make.top.equalTo(self.userNickName.mas_bottom).offset(2);
            make.right.equalTo(self);
//            make.height.mas_equalTo(25);
        }];
    }
    return self;
}

- (void)close
{
    [self removeEvent];
    
    if (_delegate && [_delegate respondsToSelector:@selector(didCloseView:identifer:view:)]) {
        [_delegate didCloseView:_row identifer:_identifer view:self];
    }
}

- (void)viewDidMoveToSuperview
{
    [super viewDidMoveToSuperview];
    
    [self addEvent];
    
    NSLog(@"viewDidMoveToSuperview");
}

- (void)viewDidMoveToWindow
{
    [super viewDidMoveToWindow];
    
    NSLog(@"viewDidMoveToWindow");
}

- (void)removeFromSuperviewWithoutNeedingDisplay
{
    [super removeFromSuperviewWithoutNeedingDisplay];
    
    NSLog(@"removeFromSuperviewWithoutNeedingDisplay");
}



- (void)dealloc
{
    NSLog(@"FriendListView dealloc");
    
    [self removeEvent];
}

- (void)mouseEntered:(NSEvent*)event
{
    NSLog(@"mouseEntered");

    self.closeBtn.hidden = NO;
}


- (void)mouseExited:(NSEvent *)event
{
//    NSLog(@"mouseExited:%@",self.userNickName.stringValue);

    NSLog(@"mouseExited");

    self.closeBtn.hidden = YES;

}

- (void)mouseMoved:(NSEvent *)event
{
    NSLog(@"mouseMoved");
}

- (void)rightMouseDown:(NSEvent *)event
{
    NSLog(@"rightMouseDown");
}

- (void)addEvent
{
    [self removeEvent];
    
    NSTrackingArea *area = [[NSTrackingArea alloc]initWithRect:self.bounds options:NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingActiveInKeyWindow owner:self userInfo:nil];
    
//    NSTrackingArea *area = [[NSTrackingArea alloc]initWithRect:self.bounds options:NSTrackingMouseEnteredAndExited  | NSTrackingActiveInKeyWindow owner:self userInfo:nil];
    
    self.area = area;
    [self addTrackingArea:area];
    
    
    //当前windows，所有NSView都收到该事件。。。
    __weak typeof(self) weakSelf = self;
    self.leftMouseDownEvent = [NSEvent addLocalMonitorForEventsMatchingMask: NSEventMaskLeftMouseDown handler:^NSEvent * _Nullable(NSEvent * _Nonnull aEvent) {

        NSPoint aPoint = [self convertPoint:[aEvent locationInWindow] fromView:self];
        if (!NSPointInRect(aPoint, self.bounds)) {

            return aEvent;
        }
        
        NSPoint clostPt = [self.closeBtn convertPoint:[aEvent locationInWindow] fromView:nil];
        if (NSPointInRect(clostPt, self.closeBtn.bounds)) {

            return aEvent;
        }
        
       
//            NSLog(@"鼠标左键单击:%@",weakSelf.userNickName.stringValue);
        
        [weakSelf didLeftMouseDown];

        return aEvent;
    }];
    
    
    self.rightMouseDownEvent = [NSEvent addLocalMonitorForEventsMatchingMask: NSEventMaskRightMouseDown handler:^NSEvent * _Nullable(NSEvent * _Nonnull aEvent) {

        NSPoint aPoint = [self convertPoint:[aEvent locationInWindow] fromView:nil];
        if (!NSPointInRect(aPoint, self.bounds)) {

            return aEvent;
        }
       
//            NSLog(@"鼠标右键单击:%@",weakSelf.userNickName.stringValue);
        [weakSelf didRightMouseDown];

        return aEvent;
    }];
    
}

- (void)didLeftMouseDown
{
    if (_delegate && [_delegate respondsToSelector:@selector(didselectRow:identifer:view:)]) {
        [_delegate didselectRow:_row identifer:_identifer view:self];
    }
}

- (void)didRightMouseDown
{
    if (_delegate && [_delegate respondsToSelector:@selector(didRightMouseDownRow:identifer:view:)]) {
        [_delegate didRightMouseDownRow:_row identifer:_identifer view:self];
    }
}

- (void)removeEvent
{
    if (self.leftMouseDownEvent) {
        [NSEvent removeMonitor:self.leftMouseDownEvent];
        self.leftMouseDownEvent = nil;
    }
    
    if (self.rightMouseDownEvent) {
        [NSEvent removeMonitor:self.rightMouseDownEvent];
        self.rightMouseDownEvent = nil;
    }
    
    if (self.area) {
        [self removeTrackingArea:self.area];
        self.area = nil;
    }
}

@end
